package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        //当前集合用于存放从begin到end范围内的每天的日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        //循环，每次日期加1，并加入集合中，直到结束日期
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //当前集合用于存放从begin到end范围内的每天对应的营业额
        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate everyday : dateList) {
            //查询每天对应的营业额,从当天00:00:00开始，到当天23:59:59结束，查询已完成的订单
            LocalDateTime beginTime = LocalDateTime.of(everyday, LocalTime.MIN);//将LocalDate+LocalTime转换成LocalDateTime
            LocalDateTime endTime = LocalDateTime.of(everyday, LocalTime.MAX);//23:59:59
            Integer status = Orders.COMPLETED;
            Double everydayTurnover = orderMapper.sumByDate(status, beginTime, endTime);
            //如果为null，则表示没有营业额,设置为0.0
            if (everydayTurnover == null) {
                everydayTurnover = 0.0;
            }
            turnoverList.add(everydayTurnover);
        }
        //封装返回数据
        TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder()
                .dateList(dateList.toString())
                .turnoverList(turnoverList.toString())
                .build();
        return turnoverReportVO;
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        //当前集合用于存放从begin到end范围内的每天的日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //当前集合用于存放从begin到end范围内的每天对应的总用户数
        List<Integer> totalUserList = new ArrayList<>();
        //当前集合用于存放从begin到end范围内的每天对应新增的用户数
        List<Integer> newUserList = new ArrayList<>();
        //循环，查询每天对应的总用户数和新增的用户数
        for (LocalDate everyday : dateList) {
            LocalDateTime everydayBegin = LocalDateTime.of(everyday, LocalTime.MIN);
            LocalDateTime everydayEnd=LocalDateTime.of(everyday, LocalTime.MAX);
            //查询每天对应的总用户数
            Integer everydayUser = orderMapper.getUserNumberByDate(everydayBegin, everydayEnd);
            totalUserList.add(everydayUser);
            //查询每天对应新增的用户数
            Integer everydayNewUser=orderMapper.getNewUserNumberByDate(everydayBegin, everydayEnd);
            newUserList.add(everydayNewUser);
        }
        //封装返回数据
        UserReportVO userReportVO = UserReportVO.builder()
                .dateList(dateList.toString())
                .totalUserList(totalUserList.toString())
                .newUserList(newUserList.toString())
                .build();
        return userReportVO;
    }
}
